// pages/Show/Show.js
let Time;
let OpenId;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    screenWidth: "",
    screenHeight: "",
    introductionHeight: false,
    introduction: "",
    titleHeight: "",
    timeHeight: "",
    time: "",
    title: "",
    day: "",
    Lists: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    wx.showShareMenu({
      withShareTicket:true
    })
    Time = options.time;
    OpenId = options.openid;
    console.log(OpenId);
    var method=options.method;

    this.getSystemInfo();
    this.Show();
    if(method=="share"){
      console.log(method)
      this.onPullDownRefresh();
    }
    
  },
  tapImage: function(e) {
    console.log(e);
    let URLS = [];
    let lists = this.data.Lists;
    let j=0;
    for (let i in lists) {
      if (lists[i].imageUrl != "") {
        URLS[j]=lists[i].imageUrl;
        j++;
      }
    }
    wx.previewImage({
      current:e.currentTarget.dataset.url,
      urls: URLS,
    })
    console.log(URLS)
  },
  bindload: function(ev) {
    console.log(ev)
    let scale = ev.detail.height / ev.detail.width;
    let lists = this.data.Lists;
    lists[ev.currentTarget.dataset.chooseid].imgHeight = this.data.screenWidth * 0.9 * 0.9 * scale;
    lists[ev.currentTarget.dataset.chooseid].listimgHide = false;
    this.setData({
      Lists: lists,
    })

    console.log(this.data.Lists)
  },
  imageErr: function(e) {

    console.log("err");
    console.log(e)
    let that = this;
    let lists = that.data.Lists;

    wx.cloud.downloadFile({
      fileID: 'cloud://gerencaipu-caiminghong.6765-gerencaipu-caiminghong-1300563066/images/menu_' + OpenId + '_' + that.data.time + '_' + e.currentTarget.dataset.chooseid + '.jpg',
      success: function(res) {
        console.log(res.tempFilePath);
        lists[e.currentTarget.dataset.chooseid].imageUrl = "" + res.tempFilePath;
        that.setData({
          Lists: lists,
        })
      },
      fail: function(err) {
        console.log(err);
      }
    })

  },
  //获取屏幕宽高
  getSystemInfo: function() {
    let that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          screenWidth: res.windowWidth,
          screenHeight: res.windowHeight,
        })
      },
    })
  },
  //从云端获取数据
  Show: function() {
    let that = this;
    wx.cloud.callFunction({
      name: "runDB",
      data: {
        db: "Menu",
        type: "get",
        condition: {
          time: parseInt(Time),
        },
        field: {
          title: true,
          introduction: true,
          steps: true,
          time: true,
          day: true,
          _id: false,
        }
      },
      success: function(res) {
        console.log(res.result.data[0])
        that.setData({
          title: res.result.data[0].title,
          Lists: res.result.data[0].steps,
          introduction: res.result.data[0].introduction,
          time: res.result.data[0].time,
          day: res.result.data[0].day,
        })
        console.log(that.data)
        if (that.data.introduction == "") {
          that.setData({
            introductionHide: true,
          })
        } else {
          that.setData({
            introductionHide: false,
          })
        }
        let steps = that.data.Lists;
        for (let i = 0; i < steps.length; i++) {
          if (steps[i].imgExist == 1) {
            steps[i].listimgHide = false;
          } else {
            steps[i].listimgHide = true;
          }
        }
        that.setData({
          Lists: steps,
        })
      },
      fail: function(err) {
        console.log(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let that = this;
    let lists = that.data.Lists;
    for (let i = 0; i < lists.length; i++) {
      if (lists[i].imageUrl == "") {
        continue;
      }
      wx.cloud.downloadFile({
        fileID: 'cloud://gerencaipu-caiminghong.6765-gerencaipu-caiminghong-1300563066/images/menu_' + OpenId + '_' + that.data.time + '_' + i + '.jpg',
        success: function(res) {
          console.log(res.tempFilePath);
          lists[i].imageUrl = "" + res.tempFilePath;
          that.setData({
            Lists: lists,
          })
        },
        fail: function(err) {
          console.log(err);
        }
      })
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: this.data.title,
      path: "/pages/Show/Show?time=" + Time + '&openid=' + OpenId+"&method="+"share",
    }
  }
})